package com.wedata.美洽;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson2.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class 美洽服务 {

    private static String uri = "https://api.meiqia.com/";
    private static String secret = "7c556cabf347b9f0332a85bad3ef5f13";

    public static void main(String[] args) throws UnsupportedEncodingException {

        JSONObject jsonObject1 = JSONObject.parseObject ("{\"id\":\"sub_4396c1bc2bcd239ae93bb686d4428efa_1742951076_3488\",\"event\":\"client.updated\",\"enterprise_token\":\"4396c1bc2bcd239ae93bb686d4428efa\",\"created_at\":1742951076,\"client\":{\"__chat_source\":\"\",\"__follow_source\":\"\",\"__openid\":\"\",\"ad_id\":\"\",\"ad_material_image_id\":\"\",\"ad_material_title_id\":\"\",\"ad_material_video_id\":\"\",\"ad_name\":\"\",\"ad_project_id\":\"\",\"ad_project_name\":\"\",\"address\":\"\",\"adgroup_id\":\"\",\"adgroup_name\":\"\",\"advertiser_id\":\"\",\"advertiser_name\":\"\",\"age\":\"\",\"avatar\":\"https://p11.douyinpic.com/aweme/720x720/aweme-avatar/tos-cn-i-0813_ogfvAQ9GEPRFIAEeTEkFgBEDE7AmgQ7AfmEAAB.jpeg?from=3782654143\",\"baidu_aduserid\":\"\",\"browser_family\":\"\",\"campaign_id\":\"\",\"campaign_name\":\"\",\"city\":\"\",\"comment\":\"\",\"contact\":\"\",\"country\":\"\",\"created_on\":\"2025-03-25T03:19:22.90\",\"creativity_id\":\"\",\"creativity_name\":\"\",\"email\":\"\",\"enterprise_id\":473598,\"gender\":\"\",\"ip\":\"\",\"keyword\":\"\",\"landing_page_title\":\"\",\"landing_page_url\":\"\",\"latest_conv_on\":\"2025-03-25T05:43:09.59\",\"main_channel\":\"douyin\",\"name\":\"消失的下雨天\",\"os_family\":\"\",\"os_language\":\"\",\"platform\":\"\",\"province\":\"\",\"qq\":\"\",\"source_domain\":\"\",\"source_keyword\":\"体德健身教练培训\",\"source_source\":\"douyin\",\"source_type\":\"mult_channel\",\"source_url\":\"\",\"sub_channel\":\"24e50bc8-e3a1-5c6d-a11d-e34afb51699d\",\"tag\":\"已留资|已挖需\",\"tags\":[423000,433524],\"tel\":\"18862819372\",\"track_id\":\"2un7lGa3pxmIkO7g1gipLOyqIok\",\"updated_on\":\"2025-03-26T01:04:35.81\",\"url\":\"\",\"utm_campaign\":\"\",\"utm_content\":\"\",\"utm_medium\":\"\",\"utm_source\":\"\",\"utm_term\":\"\",\"weibo\":\"\",\"weixin\":\"walt2k5\"}}");
        //
        JSONObject jsonObject2 = JSONObject.parseObject ("{\"id\":\"sub_4396c1bc2bcd239ae93bb686d4428efa_1743140665_1520\",\"event\":\"client.created\",\"enterprise_token\":\"4396c1bc2bcd239ae93bb686d4428efa\",\"created_at\":1743140665,\"client\":{\"__chat_source\":\"\",\"__follow_source\":\"\",\"__openid\":\"oE_5A5ix1OuZlAz9RAisM31CyQRo\",\"ad_id\":\"\",\"ad_material_image_id\":\"\",\"ad_material_title_id\":\"\",\"ad_material_video_id\":\"\",\"ad_name\":\"\",\"ad_project_id\":\"\",\"ad_project_name\":\"\",\"address\":\"\",\"adgroup_id\":\"\",\"adgroup_name\":\"\",\"advertiser_id\":\"\",\"advertiser_name\":\"\",\"age\":\"\",\"avatar\":\"\",\"baidu_aduserid\":\"\",\"browser_family\":\"\",\"campaign_id\":\"\",\"campaign_name\":\"\",\"city\":\"\",\"comment\":\"\",\"contact\":\"\",\"country\":\"\",\"created_on\":\"2025-03-28T05:44:25.39\",\"creativity_id\":\"\",\"creativity_name\":\"\",\"email\":\"\",\"enterprise_id\":473598,\"gender\":\"\",\"ip\":\"\",\"keyword\":\"\",\"landing_page_title\":\"\",\"landing_page_url\":\"\",\"main_channel\":\"\",\"name\":\"#1743140665193(体德智训)\",\"os_family\":\"\",\"os_language\":\"\",\"platform\":\"\",\"province\":\"\",\"qq\":\"\",\"source_domain\":\"\",\"source_keyword\":\"\",\"source_source\":\"\",\"source_type\":\"direct\",\"source_url\":\"\",\"sub_channel\":\"\",\"tag\":\"\",\"tel\":\"\",\"track_id\":\"2uvt27VdE2nbof9k54SxU7jS4NF\",\"updated_on\":\"2025-03-28T05:44:25.39\",\"url\":\"\",\"utm_campaign\":\"\",\"utm_content\":\"\",\"utm_medium\":\"\",\"utm_source\":\"\",\"utm_term\":\"\",\"weibo\":\"\",\"weixin\":\"\"}}");
        JSONObject jsonObject3= JSONObject.parseObject ("{\"id\":\"sub_4396c1bc2bcd239ae93bb686d4428efa_1743129085_6136\",\"event\":\"client.created\",\"enterprise_token\":\"4396c1bc2bcd239ae93bb686d4428efa\",\"created_at\":1743129085,\"client\":{\"__chat_source\":\"\",\"__follow_source\":\"\",\"__openid\":\"\",\"ad_id\":\"\",\"ad_material_image_id\":\"\",\"ad_material_title_id\":\"\",\"ad_material_video_id\":\"\",\"ad_name\":\"\",\"ad_project_id\":\"\",\"ad_project_name\":\"\",\"address\":\"\",\"adgroup_id\":\"\",\"adgroup_name\":\"\",\"advertiser_id\":\"\",\"advertiser_name\":\"\",\"age\":\"\",\"avatar\":\"https://p11.douyinpic.com/aweme/720x720/aweme-avatar/tos-cn-avt-0015_4e1d7d644407b41825b641299c98e203.jpeg?from=3782654143\",\"baidu_aduserid\":\"\",\"browser_family\":\"\",\"campaign_id\":\"\",\"campaign_name\":\"\",\"city\":\"\",\"comment\":\"\",\"contact\":\"\",\"country\":\"\",\"created_on\":\"2025-03-28T02:31:25.28\",\"creativity_id\":\"\",\"creativity_name\":\"\",\"email\":\"\",\"enterprise_id\":473598,\"gender\":\"\",\"ip\":\"\",\"keyword\":\"\",\"landing_page_title\":\"\",\"landing_page_url\":\"\",\"latest_conv_on\":null,\"main_channel\":\"douyin\",\"name\":\"婾鐵\",\"os_family\":\"\",\"os_language\":\"\",\"platform\":\"\",\"province\":\"\",\"qq\":\"\",\"source_domain\":\"\",\"source_keyword\":\"体德健身教练培训\",\"source_source\":\"douyin\",\"source_type\":\"mult_channel\",\"source_url\":\"\",\"sub_channel\":\"24e50bc8-e3a1-5c6d-a11d-e34afb51699d\",\"tag\":\"\",\"tags\":null,\"tel\":\"18314835938\",\"track_id\":\"2uvVRZZmyrWxeg0L5hudLJT1mIT\",\"updated_on\":\"2025-03-28T02:31:25.28\",\"url\":\"\",\"utm_campaign\":\"\",\"utm_content\":\"\",\"utm_medium\":\"\",\"utm_source\":\"\",\"utm_term\":\"\",\"weibo\":\"\",\"weixin\":\"\"}}");
        System.out.println(jsonObject1);
        System.out.println(signSha1(jsonObject1));
        System.out.println("f08b796e65f360beeae6abb98291246746a29a49");

        System.out.println(jsonObject2);
        System.out.println(signSha1(jsonObject2));
        System.out.println("a690097cb1ea3cabfa04d76a9a11133db2db924c");

        System.out.println(jsonObject3);
        System.out.println(signSha1(jsonObject3));

        String ss = "{\"id\":\"sub_4396c1bc2bcd239ae93bb686d4428efa_1743129085_6136\",\"event\":\"client.created\",\"enterprise_token\":\"4396c1bc2bcd239ae93bb686d4428efa\",\"created_at\":1743129085,\"client\":{\"__chat_source\":\"\",\"__follow_source\":\"\",\"__openid\":\"\",\"ad_id\":\"\",\"ad_material_image_id\":\"\",\"ad_material_title_id\":\"\",\"ad_material_video_id\":\"\",\"ad_name\":\"\",\"ad_project_id\":\"\",\"ad_project_name\":\"\",\"address\":\"\",\"adgroup_id\":\"\",\"adgroup_name\":\"\",\"advertiser_id\":\"\",\"advertiser_name\":\"\",\"age\":\"\",\"avatar\":\"https://p11.douyinpic.com/aweme/720x720/aweme-avatar/tos-cn-avt-0015_4e1d7d644407b41825b641299c98e203.jpeg?from=3782654143\",\"baidu_aduserid\":\"\",\"browser_family\":\"\",\"campaign_id\":\"\",\"campaign_name\":\"\",\"city\":\"\",\"comment\":\"\",\"contact\":\"\",\"country\":\"\",\"created_on\":\"2025-03-28T02:31:25.28\",\"creativity_id\":\"\",\"creativity_name\":\"\",\"email\":\"\",\"enterprise_id\":473598,\"gender\":\"\",\"ip\":\"\",\"keyword\":\"\",\"landing_page_title\":\"\",\"landing_page_url\":\"\",\"main_channel\":\"douyin\",\"name\":\"婾鐵\",\"os_family\":\"\",\"os_language\":\"\",\"platform\":\"\",\"province\":\"\",\"qq\":\"\",\"source_domain\":\"\",\"source_keyword\":\"体德健身教练培训\",\"source_source\":\"douyin\",\"source_type\":\"mult_channel\",\"source_url\":\"\",\"sub_channel\":\"24e50bc8-e3a1-5c6d-a11d-e34afb51699d\",\"tag\":\"\",\"tags\":null,\"tel\":\"18314835938\",\"track_id\":\"2uvVRZZmyrWxeg0L5hudLJT1mIT\",\"updated_on\":\"2025-03-28T02:31:25.28\",\"url\":\"\",\"utm_campaign\":\"\",\"utm_content\":\"\",\"utm_medium\":\"\",\"utm_source\":\"\",\"utm_term\":\"\",\"weibo\":\"\",\"weixin\":\"\"}}";
        byte[] bytes2 = DigestUtil.sha1(ss+secret);
        //3205af0c00e850c60a750db788915719d48c31f0
        System.out.println(byteToString(bytes2));

        byte[] bytes3 = DigestUtil.sha1(jsonObject3+secret);
        //3205af0c00e850c60a750db788915719d48c31f0
        System.out.println(byteToString(bytes3));
        //订阅事件();
    }

    public static void 查询对话信息() {
        Map<String,String> body = new HashMap<>();
        body.put("enterprise_id","97631");
        body.put("limit", "10");
        body.put("page_token","1111111");
        body.put("from_tm", String.valueOf(new Date().getTime()/1000));
        body.put("to_tm", String.valueOf(new Date().getTime()/1000));
        signMd5(new JSONObject(body));

        //JSONObject result = HttpUtils.doGet(uri+"v2/conversations",body);
        //System.out.println(result);
    }

    /**
     * 这是回调我们自己的系统
     */
    public static void 订阅事件() throws UnsupportedEncodingException {
        JSONObject header = new JSONObject();
        JSONObject body = new JSONObject();
        body.put("event", "conversation.closed");
        body.put("id","1111111111111L");
        body.put("enterprise_token",1);
        body.put("created_at", new Date().getTime()/1000);
        JSONObject conversation = new JSONObject();
        conversation.put("enterprise_id",1);
        conversation.put("dev_client_id",1);
        conversation.put("page_from_url",1);
        conversation.put("search_engine",1);
        body.put("conversation", conversation);
        body = JSONObject.parseObject("{\"id\":\"sub_4396c1bc2bcd239ae93bb686d4428efa_1742950929_9426\",\"event\":\"conversation.closed\",\"enterprise_token\":\"4396c1bc2bcd239ae93bb686d4428efa\",\"created_at\":1742950929,\"conversation\":{\"enterprise_id\":473598,\"page_from_url\":\"\",\"page_land_url\":\"\",\"page_land_title\":\"\",\"page_conv_url\":\"\",\"page_conv_title\":\"\",\"search_engine_name\":\"douyin\",\"search_engine_kw\":\"体德健身教练培训\",\"visitor_ip\":\"\",\"visitor_os\":\"\",\"visitor_browser\":\"\",\"visitor_tags\":[],\"client_id\":7535855970,\"client_info\":{\"__chat_source\":\"\",\"__follow_source\":\"\",\"__openid\":\"\",\"_m_track_id\":\"2unzg6fq4ddEPPFEQq7SIx3ldKq\",\"ad_id\":\"0\",\"ad_material_image_id\":\"\",\"ad_material_title_id\":\"\",\"ad_material_video_id\":\"\",\"ad_name\":\"\",\"ad_project_id\":\"\",\"ad_project_name\":\"\",\"address\":\"\",\"adgroup_id\":\"\",\"adgroup_name\":\"\",\"advertiser_id\":\"1685689847093263\",\"advertiser_name\":\"体德健身教练培训\",\"age\":0,\"avatar\":\"https://p11.douyinpic.com/aweme/720x720/aweme-avatar/tos-cn-i-0813c001_oU5AxBbDKAPHM0OGQQEh7RAhARIReBBAnGAe6f.jpeg?from=3782654143\",\"baidu_aduserid\":\"\",\"campaign_id\":\"1827490305731787\",\"campaign_name\":\"aweme_promotion_ad_1742830611984035\",\"comment\":\"\",\"contact\":\"\",\"creativity_id\":\"1827490305731803\",\"creativity_name\":\"\",\"email\":\"\",\"enterprise_id\":473598,\"gender\":\"\",\"name\":\"菜牙\",\"qq\":\"\",\"tel\":\"\",\"track_id\":\"2unzg6fq4ddEPPFEQq7SIx3ldKq\",\"visitor_name\":\"菜牙\",\"weibo\":\"\",\"weixin\":\"\"},\"agent_account\":\"ent-473598-1740119700@mpush-robots.meiqia.com\",\"agent_name\":\"体德助手\",\"owner_id\":2189336,\"agent_ID\":\"\",\"agent_nick_name\":\"体德助手\",\"owner_realname\":\"体德助手\",\"assignee_name\":\"体德助手\",\"group_id\":976844,\"group_name\":\"__MPUSH_ROBOTS__\",\"conv_id\":7518520702,\"conv_start_tm\":\"2025-03-26 08:16:43.695920\",\"conv_end_tm\":\"2025-03-26 09:02:08.879753\",\"conv_first_resp_wait_in_secs\":0,\"conv_content\":[{\"from\":\"bot\",\"timestamp\":\"2025-03-26 08:16:48.105158\",\"content\":\"哈喽同学\uD83D\uDE4B\u200D♂我们是体德健身教练培训学校~\\n你想提高健身水平，还是考证做教练？发序号也可以，直接问我也行～\\n\\n1.我想了解线下培训\\n2.考健身教练证\\n3.我想了解课程表\\n4.全国校区分别在哪？\",\"type\":\"message\"},{\"from\":\"bot\",\"timestamp\":\"2025-03-26 08:17:08.873985\",\"content\":\"同学，你是想了解线下培训吗？\",\"type\":\"message\"}],\"conv_agent_msg_count\":2,\"conv_visitor_msg_count\":0,\"conv_quality_grade\":\"\",\"conv_leads\":\"\",\"platform\":\"\",\"summary_content\":\"\",\"summary_update_at\":\"\",\"source_type\":\"指定来源\",\"source_field\":\"douyin\",\"agent_resp_duration\":\"4.409238s\",\"effective\":\"无效\",\"missed\":\"-\",\"converse_duration\":\"20s\",\"app_name\":\"\",\"main_channel\":\"douyin\",\"main_channel_name\":\"抖音\",\"sub_channel\":\"24e50bc8-e3a1-5c6d-a11d-e34afb51699d\",\"sub_channel_name\":\"体德健身教练培训\",\"search_engine\":\"\",\"advertise_info\":{}}}");
        header.put("Meiqia-Signature", signSha1(body));

        System.out.println(body);
        System.out.println( signSha1(body));
        //HttpUtils.sendPost("http://localhost:8090/datacenter/api/meiqia/notify",header,body);
        //HttpUtils.sendPost("https://twedata.gymcity.com.cn/datacenter/api/meiqia/notify",header,body);

    }
    //signature=
    // fd32c7335202685680c7c983712536363f365628,newSign=
    // a5cc3f9eb17fe427fc1b120e0e14e188e06a0a9f
    // ymxoODvaHNgUJkhClcwwgquej6c=,
    // bizXj9p6JhsWG4zagKxJJbVmokI=
    // 请求参数:{"id":"sub_4396c1bc2bcd239ae93bb686d4428efa_1742950929_9426","event":"conversation.closed","enterprise_token":"4396c1bc2bcd239ae93bb686d4428efa","created_at":1742950929,"conversation":{"enterprise_id":473598,"page_from_url":"","page_land_url":"","page_land_title":"","page_conv_url":"","page_conv_title":"","search_engine_name":"douyin","search_engine_kw":"体德健身教练培训","visitor_ip":"","visitor_os":"","visitor_browser":"","visitor_tags":[],"client_id":7535855970,"client_info":{"__chat_source":"","__follow_source":"","__openid":"","_m_track_id":"2unzg6fq4ddEPPFEQq7SIx3ldKq","ad_id":"0","ad_material_image_id":"","ad_material_title_id":"","ad_material_video_id":"","ad_name":"","ad_project_id":"","ad_project_name":"","address":"","adgroup_id":"","adgroup_name":"","advertiser_id":"1685689847093263","advertiser_name":"体德健身教练培训","age":0,"avatar":"https://p11.douyinpic.com/aweme/720x720/aweme-avatar/tos-cn-i-0813c001_oU5AxBbDKAPHM0OGQQEh7RAhARIReBBAnGAe6f.jpeg?from=3782654143","baidu_aduserid":"","campaign_id":"1827490305731787","campaign_name":"aweme_promotion_ad_1742830611984035","comment":"","contact":"","creativity_id":"1827490305731803","creativity_name":"","email":"","enterprise_id":473598,"gender":"","name":"菜牙","qq":"","tel":"","track_id":"2unzg6fq4ddEPPFEQq7SIx3ldKq","visitor_name":"菜牙","weibo":"","weixin":""},"agent_account":"ent-473598-1740119700@mpush-robots.meiqia.com","agent_name":"体德助手","owner_id":2189336,"agent_ID":"","agent_nick_name":"体德助手","owner_realname":"体德助手","assignee_name":"体德助手","group_id":976844,"group_name":"__MPUSH_ROBOTS__","conv_id":7518520702,"conv_start_tm":"2025-03-26 08:16:43.695920","conv_end_tm":"2025-03-26 09:02:08.879753","conv_first_resp_wait_in_secs":0,"conv_content":[{"from":"bot","timestamp":"2025-03-26 08:16:48.105158","content":"哈喽同学🙋‍♂我们是体德健身教练培训学校~\n你想提高健身水平，还是考证做教练？发序号也可以，直接问我也行～\n\n1.我想了解线下培训\n2.考健身教练证\n3.我想了解课程表\n4.全国校区分别在哪？","type":"message"},{"from":"bot","timestamp":"2025-03-26 08:17:08.873985","content":"同学，你是想了解线下培训吗？","type":"message"}],"conv_agent_msg_count":2,"conv_visitor_msg_count":0,"conv_quality_grade":"","conv_leads":"","platform":"","summary_content":"","summary_update_at":"","source_type":"指定来源","source_field":"douyin","agent_resp_duration":"4.409238s","effective":"无效","missed":"-","converse_duration":"20s","app_name":"","main_channel":"douyin","main_channel_name":"抖音","sub_channel":"24e50bc8-e3a1-5c6d-a11d-e34afb51699d","sub_channel_name":"体德健身教练培训","search_engine":"","advertise_info":{}}}

    /**
     * 签名
     * @param body
     * @return
     */
    private static String signSha1(JSONObject body) throws UnsupportedEncodingException {
        byte[] byteArray = DigestUtil.sha1(body+secret);
        //return new String(Base64.getEncoder().encode(byteArray));
        return byteToString(byteArray);
    }
    /**
     * 签名
     * @param body
     * @return
     */
    private static String signMd5(JSONObject body){
        byte[] bytes = DigestUtil.md5(body+secret);
        //return new String(Base64.getEncoder().encode(byteArray));
        return byteToString(bytes);
    }
    private static String byteToString(byte[] bytes){
        StringBuilder sign = new StringBuilder();
        byte[] var4 = bytes;
        int var5 = bytes.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            byte b = var4[var6];
            String hex = Integer.toHexString(b & 255);
            if (hex.length() == 1) {
                sign.append("0");
            }

            sign.append(hex);
        }
        return sign.toString();
    }

}
